package com.imooc.pan.server.modules.recycle.controller;

import com.google.common.base.Splitter;
import com.imooc.pan.core.constants.RPanConstants;
import com.imooc.pan.core.response.R;
import com.imooc.pan.core.utils.IdUtil;
import com.imooc.pan.server.common.utils.UserIdUtil;
import com.imooc.pan.server.modules.file.entity.RPanUserFile;
import com.imooc.pan.server.modules.file.po.DeleteFilePO;
import com.imooc.pan.server.modules.file.vo.RPanUserFileVo;
import com.imooc.pan.server.modules.recycle.context.DeleteContext;
import com.imooc.pan.server.modules.recycle.context.QueryRecycleFileContext;
import com.imooc.pan.server.modules.recycle.context.RestoreContext;
import com.imooc.pan.server.modules.recycle.po.DeletePO;
import com.imooc.pan.server.modules.recycle.po.RestorePO;
import com.imooc.pan.server.modules.recycle.service.RecycleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hhb
 * @data 2024/6/15 22:24
 */
@RequestMapping
@RestController
@Api(tags = "回收站模块")
@Validated
public class RecycleController {

    @Autowired
    private RecycleService recycleService;

    @ApiOperation(value = "查询回收文件列表")
    @GetMapping("recycles")
    public R<List<RPanUserFileVo>> getRecycles() {
        QueryRecycleFileContext context = new QueryRecycleFileContext();
        context.setUserId(UserIdUtil.get());
        List<RPanUserFileVo> result = recycleService.getRecycleFiles(context);
        return R.data(result);
    }


    @ApiOperation(value = "文件还原")
    @PutMapping("/recycle/restore")
    public R restore(@Validated @RequestBody RestorePO restorePO) {
        RestoreContext context = new RestoreContext();
        String fileIds = restorePO.getFileIds();
        //转换fileIds
        List<Long> fileIdList = Splitter.on(RPanConstants.COMMON_SEPARATOR).splitToList(fileIds).stream().map(IdUtil::decrypt).collect(Collectors.toList());
        context.setFileIdList(fileIdList);
        context.setUserId(UserIdUtil.get());
        recycleService.restore(context);
        return R.success();
    }


    @ApiOperation(value = "彻底删除文件")
    @DeleteMapping("recycle")
    public R delete(@Validated @RequestBody DeletePO deletePO){
        DeleteContext context = new DeleteContext();
        String fileIds = deletePO.getFileIds();
        //转换fileIds
        List<Long> fileIdList = Splitter.on(RPanConstants.COMMON_SEPARATOR).splitToList(fileIds).stream().map(IdUtil::decrypt).collect(Collectors.toList());
        context.setFileIdList(fileIdList);
        context.setUserId(UserIdUtil.get());
        recycleService.delete(context);
        return R.success();
    }
}

